<?php

namespace App\Rep;

use App\Models\Purchase;
use App\Models\PurchaseChild;
use App\Models\WarehouseOrder;
use App\Models\WarehouseOrderChild;
use Illuminate\Support\Facades\DB;

class PurchaseRep
{

    //获取采购单列表
    public function getPurchaseListRep($params){
        $list = Purchase::Search($params)
            ->with(['child'=>function($q){
                $q->with(['material','supplier','warehouse']);
            },'buyers','order'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??1000);

        return $list;
    }

    //添加采购单
    public function createPurchaseRep($params){
        DB::beginTransaction();
        try {
            if(!empty($params['id'])){
                //编辑
                $purchase = Purchase::where('id',$params['id'])->first();
            }else{
                $purchase = new Purchase();
            }
            $purchase->purchase_number = $params['purchase_number'];
            $purchase->purchase_date = $params['purchase_date'];
            $purchase->delivery_date = $params['delivery_date'];
            $purchase->buyer = $params['buyer'];
            $purchase->save();
            foreach($params['child'] as $k=>$v){
                if(isset($v['id']) && !empty($params['id'])){
                    //编辑
                    $purchase_child = PurchaseChild::where('id',$v['id'])->first();
                }else{
                    $purchase_child = new PurchaseChild();
                }
                $purchase_child->purchase_id = $purchase->id;
                $purchase_child->material_id = $v['material_id'];
                $purchase_child->num = $v['num'];
//                $purchase_child->model_number_id = $v['model_number_id'];
//                $purchase_child->specification_id = $v['specification_id'];
//                $purchase_child->color_id = $v['color_id'];
                $purchase_child->remark = $v['remark']??'';
                $purchase_child->supplier_id = $v['supplier_id'];//供应商
                $purchase_child->price = $v['price'];
                $purchase_child->warehouse_id = $v['warehouse_id'];
                $purchase_child->save();
            }

            DB::commit();
            return ['code'=>0,'data'=>$purchase,'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }
    }

    public function setPurchaseStatusRep($id,$status,$user_id){
        DB::beginTransaction();
        try {
            $purchase = Purchase::find($id);
            $purchase_child = PurchaseChild::where('purchase_id',$id)->get();
            $purchase->status = $status;
            $purchase->save();
            if($status == 1){
                $arr = [
                    'type'=>1,//1:入库单
                    'clearing_form'=>1,//结算方式
                    'warehouse_id'=>'',//库房id
                    'buyer'=>$purchase->buyer,//采购人id
                    'supply_order_number'=>'',//供应单号
                    'warehouse_keeper'=>'',//仓库管理员id
                    'finance'=>'',//财务id
                    'prepared_by'=>$user_id,//制单人
                    'warehouse_entry_time'=>date("Y-m-d H:i:s"),//入库时间
                    'remark'=>'采购单完成，自动生成入库单',//备注
                    //'child'=>[]
                ];
                $warehouse_order = WarehouseOrder::create($arr);
                $num = 0;
                $money = 0;
                foreach($purchase_child as $k=>$v){
                    $insert = [];
                    $insert = [
                        'type'=>1,
                        'warehouse_order_id'=>$warehouse_order->id,
                        'serial_number'=>$k+1,
                        'material_id'=>$v->material_id,//材料id
                        'num'=>$v->num,//数量
                        'unit_price'=>$v->price,//单价
                        'money'=>$v->num*$v->unit_price,
                        'remark'=>'采购单完成，自动生成入库单',//备注
                        'supplier_id'=>$v->supplier_id//供应商
                    ];
                    WarehouseOrderChild::create($insert);
                    //计算数量，金额合计
                    $num += $v->num;
                    $money += $v->money;
                }
                WarehouseOrder::where('id',$warehouse_order->id)->update(['num'=>$num,'money'=>$money]);
            }
            DB::commit();
            return ['code'=>0,'data'=>$purchase,'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }
    }
}
